﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using FireDragon.GameObjects;

namespace FireDragon
{
    public partial class Page : UserControl
    {
        double x = 500;
        bool _isDrang = false;
        Point _olPoint;
        int _drangSpeed = 10;
        double _centerX, _centerY;
        double _mapWidth, _MapHeight;
        double _cameraWidth, _cameraHeight;
        double _xOffset, _yOffset;
        //var for drop and drang hinh dai dien
        bool _isHDDClick = false;
        Point _hddOldPoint;
        HinhDaiDien _hdd;
        CongTrinh _congTrinh;
        //var for item daidien
        double _hdWidth, _hddHeight, _padding, _cach;
        int id=0;
        public Page()
        {
            InitializeComponent();
            _centerX = 2000;
            _centerY = 300;
            _mapWidth = _MapHeight = 4000;
            _cameraWidth = 1000;
            _cameraHeight = 500;
            _xOffset = (double)cavanMap.GetValue(Canvas.LeftProperty);
            _yOffset = (double)cavanMap.GetValue(Canvas.TopProperty);
            _hdWidth = 90;
            _padding = 10;
            _cach = 5;
            LoadHinhDaiDien();
        }
        void CameraMove(double dx, double dy)
        {
            double x, y, maxXOffset, maxYOffset, ym;
            x = (double)cavanMap.GetValue(Canvas.LeftProperty);
            y = (double)cavanMap.GetValue(Canvas.TopProperty);
            _xOffset = x + dx / _drangSpeed;
            _yOffset = y + dy / _drangSpeed;
            if (_xOffset > 0)
                _xOffset = 0;
            if (_yOffset > 0)
                _yOffset = 0;
            maxXOffset = -(_mapWidth - _cameraWidth);
            maxYOffset = -(_MapHeight - _cameraWidth);
            if (_xOffset <= maxXOffset)
                _xOffset = maxXOffset;
            if (_yOffset <= maxYOffset)
                _yOffset = maxYOffset;
            cavanMap.SetValue(Canvas.LeftProperty, _xOffset);
            cavanMap.SetValue(Canvas.TopProperty, _yOffset);


        }

        private void cavanCamera_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //MessageBox.Show("click");
            _olPoint = e.GetPosition(null);
            _isDrang = true;
           

        }

        private void cavanCamera_MouseMove(object sender, MouseEventArgs e)
        {
            if (!_isDrang || _isHDDClick)
                return;
            Point newPoint = e.GetPosition(null);
            double dx, dy;
            dx = (newPoint.X - _olPoint.X) / _drangSpeed;
            dy = (newPoint.Y - _olPoint.Y) / _drangSpeed;
            CameraMove(dx, dy);
        }

        private void cavanCamera_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            _isDrang = false;
        }

        private void HinhDaiDien_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

            HinhDaiDien item = sender as HinhDaiDien;
            switch (item.type.ToString())
            {
                case "ruong":
                    _congTrinh = new Ruong();
                    _congTrinh.MouseLeftButtonDown += CongTrinh_MouseLeftButtonDown;
                    _congTrinh.Name = "ruong" + id;
                    id++;
                    break;
                case "nha":
                    _congTrinh = new Nha();
                    _congTrinh.MouseLeftButtonDown += CongTrinh_MouseLeftButtonDown;
                    _congTrinh.Name = "nha" + id;
                    id++;
                    break;

            }
            _isHDDClick = true;
          
            cavanMap.Children.Add(_congTrinh);
            _olPoint = GetPositionOfHinhDaiDien(sender);
            SetPosition(_congTrinh, _olPoint);


        }
        private void CongTrinh_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            _isDrang = false;
           CongTrinh tam=sender as CongTrinh;
           tam.SetValue(Canvas.ZIndexProperty, 99);
           tam.HienThiThongTin();
        }

        private void HinhDaiDien_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            _isHDDClick = false;
        }

        private void HinhDaiDien_MouseMove(object sender, MouseEventArgs e)
        {
            if (_isHDDClick)
            {
                Point newp = e.GetPosition(null);
                double dx, dy;
                dx = newp.X - _olPoint.X;
                dy = newp.Y - _olPoint.Y;
                SetPosition(_congTrinh, _olPoint.X + dx - _xOffset, _olPoint.Y + dy - _yOffset);
            }
        }


        Point GetPositionOfHinhDaiDien(object sender)
        {
            HinhDaiDien item = sender as HinhDaiDien;
            item.CaptureMouse();
            double x, y;
            x = (double)(cavanBot.GetValue(Canvas.LeftProperty)) + (double)(cavanHinhDaiDien.GetValue(Canvas.LeftProperty)) + (double)(item.GetValue(Canvas.LeftProperty)) - _xOffset;
            y = (double)(cavanBot.GetValue(Canvas.TopProperty)) + (double)(cavanHinhDaiDien.GetValue(Canvas.TopProperty)) + (double)(item.GetValue(Canvas.TopProperty)) - _yOffset;
            return new Point(x, y);
        }
        void SetPosition(UIElement item, Point vt)
        {
            item.SetValue(Canvas.LeftProperty, vt.X);
            item.SetValue(Canvas.TopProperty, vt.Y);

        }
        void SetPosition(UIElement item, double x, double y)
        {
            item.SetValue(Canvas.LeftProperty, x);
            item.SetValue(Canvas.TopProperty, y);

        }

        //them hinh dai dien vao kung bot
        void AddHinhDaiDien(HinhDaiDien hd, int vt)
        {
            cavanHinhDaiDien.Children.Add(hd);
            double x, y;
            x = (_hdWidth + _cach) * vt + _padding;
            hd.SetValue(Canvas.LeftProperty, x);
        }
        void AddHandleforHinhDaiDien(HinhDaiDien hd)
        {
            hd.MouseLeftButtonDown += HinhDaiDien_MouseLeftButtonDown;
            hd.MouseLeftButtonUp += HinhDaiDien_MouseLeftButtonUp;
            hd.MouseMove += HinhDaiDien_MouseMove;
        }
        void LoadHinhDaiDien()
        {
            int i=0;
            for (; i < 3; i++)
            {
                HinhDaiDienRuong r = new HinhDaiDienRuong();
                AddHandleforHinhDaiDien(r);
                AddHinhDaiDien(r, i);
            }
            for (; i < 6; i++)
            {
                HinhDaiDienNha r = new HinhDaiDienNha();
                AddHandleforHinhDaiDien(r);
                AddHinhDaiDien(r, i);
            }
        }

    }
}
